package com.supcon.exchangedata.service.rq;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.supcon.exchangedata.pojo.entity.mysql.operation.rq.GasLiquidInfo;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import com.supcon.exchangedata.repository.master.MasterStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave1.SlaveOneStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave5.SlaveGasLiquidInfo;
import com.supcon.exchangedata.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Slf4j
public class GasLiquidInfoService {
    @Resource
    private SlaveGasLiquidInfo slaveGasLiquidInfo;
    /**
     * 天表服务
     */
    @Resource
    private MasterStatistOtherParametersDayRepository dayRepository;

    @Resource
    private SlaveOneStatistOtherParametersDayRepository slaveOneStatistOtherParametersDayRepository;

    public static String gasLiquidSupply15="30020";
    public static String gasLiquidSupply50="30021";
    public static String gasLiquidAmount="30003";

    public Map<String, Double> handleData(List<StatistOtherParametersDay> otherHours) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        HashMap<String, Double> objectObjectHashMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(otherHours)) {
            otherHours.forEach((e) -> {
                LocalDateTime collectTime = e.getLastTime();
                String key = timeFormatter.format(collectTime);
                Double inAvgValue = e.getInAvgValue();
                objectObjectHashMap.put(key, inAvgValue);

            });
        }
        return objectObjectHashMap;
    }

    public  void newLiqIdInfo(){
        LocalDateTime localDateTime = LocalDate.now().atStartOfDay();

        for (int i = 0; i < 7; i++) {
            LocalDateTime dateTimeTarget = localDateTime.minusDays(i);
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            GasLiquidInfo topData = slaveGasLiquidInfo.findByDataTime(dateTimeTarget);
            if (topData==null){
                StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType = slaveOneStatistOtherParametersDayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, 30020L);
                StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType1 = slaveOneStatistOtherParametersDayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, 30021L);
                StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType2 = slaveOneStatistOtherParametersDayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(dateTimeTarget, 3L, 30003L);
                if ((distinctByDayOfMonthKeyAndObjIdAndType == null || distinctByDayOfMonthKeyAndObjIdAndType.getInAvgValue() == null) && (distinctByDayOfMonthKeyAndObjIdAndType1 == null || distinctByDayOfMonthKeyAndObjIdAndType1.getInAvgValue() == null) &&
                        (distinctByDayOfMonthKeyAndObjIdAndType2 == null || distinctByDayOfMonthKeyAndObjIdAndType2.getInAvgValue() == null)) {
                    continue;
                }
                GasLiquidInfo info = new GasLiquidInfo();
                Timestamp timestamp = DateTimeUtils.localDateTimeToTimeSTamp(LocalDateTime.now());
                info.setTongTime(timestamp);
                info.setBizTime(DateTimeUtils.localDateTimeToTimeSTamp(dateTimeTarget));
                info.setOp("insert");
                info.setDataTime(dateTimeTarget);
                info.setGasLiquidAmount(distinctByDayOfMonthKeyAndObjIdAndType2.getInAvgValue());
                info.setGasLiquidSupply50(distinctByDayOfMonthKeyAndObjIdAndType1.getInAvgValue());
                info.setGasLiquidSupply15(distinctByDayOfMonthKeyAndObjIdAndType.getInAvgValue());
                info.setDay(timeFormatter.format(dateTimeTarget));
                slaveGasLiquidInfo.save(info);
            }
        }
    }
    public void pushGasLiquidInfo(){
        List<GasLiquidInfo> all = slaveGasLiquidInfo.findAll();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Timestamp timestamp = DateTimeUtils.localDateTimeToTimeSTamp(LocalDateTime.now());
        if (CollectionUtils.isEmpty(all)){
            //如果是第一次同步则需要同步全部数据
            List<StatistOtherParametersDay> list1 = dayRepository.findByType(gasLiquidSupply15);
            List<StatistOtherParametersDay> list2 = dayRepository.findByType(gasLiquidSupply50);
            List<StatistOtherParametersDay> list3 = dayRepository.findByType(gasLiquidAmount);
            Map<String, Double> map1 = handleData(list1);
            Map<String, Double> map2 = handleData(list2);
            Map<String, Double> map3 = handleData(list3);
            List<GasLiquidInfo> objects = new ArrayList<>();
            list1.forEach(v->{
                GasLiquidInfo gasLiquidInfo = new GasLiquidInfo();
                LocalDateTime lastTime = v.getLastTime();
                String key = timeFormatter.format(lastTime);
                if (map1.containsKey(key)){
                    gasLiquidInfo.setGasLiquidSupply15(v.getInAvgValue());
                }
                if (map2.containsKey(key)){
                    gasLiquidInfo.setGasLiquidSupply50(v.getInAvgValue());
                }
                if (map3.containsKey(key)){
                    gasLiquidInfo.setGasLiquidAmount(v.getInAvgValue());
                }
                gasLiquidInfo.setTongTime(timestamp);
                gasLiquidInfo.setBizTime(DateTimeUtils.localDateTimeToTimeSTamp(LocalDateTime.now()));
                gasLiquidInfo.setOp("insert");
                gasLiquidInfo.setDay(key);
                gasLiquidInfo.setDataTime(lastTime);
                objects.add(gasLiquidInfo);
            });
            slaveGasLiquidInfo.saveAll(objects);
        }else {
            //获取当前时间
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime dayOfMonthKey = LocalDate.parse(new Date().toString(), dateTimeFormatter).atStartOfDay();
            LocalDateTime now = LocalDateTime.now();
            String format = timeFormatter.format(now);
            //先查询前置库是否有数据
            GasLiquidInfo byDay = this.slaveGasLiquidInfo.findByDay(format);
            if (null == byDay){
                //获取我们数据库的数据
                StatistOtherParametersDay one1 = dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L, 30020L,dayOfMonthKey);
                StatistOtherParametersDay one2 = dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30021L, dayOfMonthKey);
                StatistOtherParametersDay one3 = dayRepository.findByObjIdAndTypeAndDayOfMonthKey(3L,30003L, dayOfMonthKey);
                GasLiquidInfo gasLiquidInfo = new GasLiquidInfo();
                gasLiquidInfo.setGasLiquidSupply15(one1.getInAvgValue());
                gasLiquidInfo.setGasLiquidSupply50(one2.getInAvgValue());
                gasLiquidInfo.setGasLiquidAmount(one3.getInAvgValue());
                gasLiquidInfo.setTongTime(timestamp);
                gasLiquidInfo.setBizTime(timestamp);
                gasLiquidInfo.setOp("insert");
                gasLiquidInfo.setDay(format);
                gasLiquidInfo.setDataTime(now);
                slaveGasLiquidInfo.save(gasLiquidInfo);
            }
        }
    }
}
